﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace YunShop_WebAPI.BLL
{
    public static class YunShopSecurity
    {
        private static string AESKey = "k&WX@)!%09!AC8WB";
        /// <summary>
        /// AES加密(DataTable)
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="ColName"></param>
        /// <returns></returns>
        public static string EncryptDt(DataTable dt, string SouName, string TagName)
        {
            if (!dt.Columns.Contains(SouName))
            {
                return string.Format("表{0}不存在列{1}", dt.TableName, SouName);
            }
            if (!dt.Columns.Contains(TagName))
            {
                return string.Format("表{0}不存在列{1}", dt.TableName, TagName);
            }
            foreach (DataRow item in dt.Rows)
            {
                item[TagName] = Encrypt(item[SouName].ToString());
            }
            return "";
        }

        /// <summary>
        /// AES加密(Row)
        /// </summary>
        /// <param name="item"></param>
        /// <param name="ColName"></param>
        public static string EncryptRow(DataRow item, string SouName, string TagName)
        {
            if (!item.Table.Columns.Contains(SouName))
            {
                return string.Format("行不存在列{0}", SouName);
            }
            if (!item.Table.Columns.Contains(TagName))
            {
                return string.Format("行不存在列{0}", TagName);
            }
            try
            {
                item[TagName] = Encrypt(item[SouName].ToString());
                return "";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        /// <summary>
        /// AES加密
        /// </summary>
        /// <param name="encryptStr">明文</param>
        /// <param name="key">密钥</param>
        /// <returns></returns>
        public static string Encrypt(string encryptStr)
        {
            string key = AESKey;
            byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key);
            byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(encryptStr);
            RijndaelManaged rDel = new RijndaelManaged();
            rDel.Key = keyArray;
            rDel.Mode = CipherMode.ECB;
            rDel.Padding = PaddingMode.PKCS7;
            ICryptoTransform cTransform = rDel.CreateEncryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
            return Convert.ToBase64String(resultArray, 0, resultArray.Length);
        }

        /// <summary>
        /// AES解密(DataTable)
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="ColName"></param>
        /// <returns></returns>
        public static string DecryptDt(DataTable dt, string SouName, string TagName)
        {
            if (!dt.Columns.Contains(SouName))
            {
                return string.Format("表{0}不存在列{1}", dt.TableName, SouName);
            }
            if (!dt.Columns.Contains(TagName))
            {
                return string.Format("表{0}不存在列{1}", dt.TableName, TagName);
            }
            foreach (DataRow item in dt.Rows)
            {
                try
                {
                    string key = AESKey;
                    byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key);
                    byte[] toEncryptArray = Convert.FromBase64String(item[SouName].ToString());
                    RijndaelManaged rDel = new RijndaelManaged();
                    rDel.Key = keyArray;
                    rDel.Mode = CipherMode.ECB;
                    rDel.Padding = PaddingMode.PKCS7;
                    ICryptoTransform cTransform = rDel.CreateDecryptor();                   
                    try
                    {
                        byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
                        item[TagName] = UTF8Encoding.UTF8.GetString(resultArray);
                    }
                    catch
                    {
                        item[TagName] = "0";
                    }
                }
                catch (Exception ex)
                {
                    return ex.Message;
                }
            }
            return "";
        }

        /// <summary>
        /// AES解密(Row)
        /// </summary>
        /// <param name="item"></param>
        /// <param name="ColName"></param>
        public static string DecryptRow(DataRow item, string SouName, string TagName)
        {
            if (!item.Table.Columns.Contains(SouName))
            {
                return string.Format("行不存在列{0}", SouName);
            }
            if (!item.Table.Columns.Contains(TagName))
            {
                return string.Format("行不存在列{0}", TagName);
            }
            try
            {
                string key = AESKey;
                byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key);
                byte[] toEncryptArray = Convert.FromBase64String(item[SouName].ToString());
                RijndaelManaged rDel = new RijndaelManaged();
                rDel.Key = keyArray;
                rDel.Mode = CipherMode.ECB;
                rDel.Padding = PaddingMode.PKCS7;
                ICryptoTransform cTransform = rDel.CreateDecryptor();                
                try
                {
                    byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
                    item[TagName] = UTF8Encoding.UTF8.GetString(resultArray);
                }
                catch
                {
                    item[TagName] = "0";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
            return "";
        }

        /// <summary>
        /// AES解密
        /// </summary>
        /// <param name="decryptStr">密文</param>
        /// <param name="key">密钥</param>
        /// <returns></returns>
        public static string Decrypt(string decryptStr)
        {
            try
            {
                string key = AESKey;
                byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key);
                byte[] toEncryptArray = Convert.FromBase64String(decryptStr);
                RijndaelManaged rDel = new RijndaelManaged();
                rDel.Key = keyArray;
                rDel.Mode = CipherMode.ECB;
                rDel.Padding = PaddingMode.PKCS7;
                ICryptoTransform cTransform = rDel.CreateDecryptor();
                byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
                return UTF8Encoding.UTF8.GetString(resultArray);
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
        
        /// <summary>
        /// AES解密
        /// </summary>
        /// <param name="decryptStr">密文</param>
        /// <param name="key">密钥</param>
        /// <returns></returns>
        public static decimal DecryptToDecimal(string decryptStr)
        {
            try
            {
                string key = AESKey;
                byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key);
                byte[] toEncryptArray = Convert.FromBase64String(decryptStr);
                RijndaelManaged rDel = new RijndaelManaged();
                rDel.Key = keyArray;
                rDel.Mode = CipherMode.ECB;
                rDel.Padding = PaddingMode.PKCS7;
                ICryptoTransform cTransform = rDel.CreateDecryptor();
                byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
                return Convert.ToDecimal(UTF8Encoding.UTF8.GetString(resultArray));
            }
            catch
            {
                return 0;
            }
        }
    }
}
